package com.csun.cmny.provider.controller.admin;

import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.domain.UacMenu;
import com.csun.cmny.provider.model.domain.UacRole;
import com.csun.cmny.provider.model.dto.action.ActionMainQueryDto;
import com.csun.cmny.provider.model.dto.menu.MenuQueryDto;
import com.csun.cmny.provider.model.dto.role.CheckRoleCodeDto;
import com.csun.cmny.provider.model.vo.BindAuthVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.UacMenuService;
import com.csun.cmny.provider.service.UacRoleService;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 角色管理-公共方法.
 *
 * @author paascloud.net @gmail.com
 */
@RestController
@RequestMapping(value = "/role", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Api(value = "Web - UacRoleCommonController", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class UacRoleCommonController extends BaseController {

	@Resource
	private UacRoleService uacRoleService;
	@Resource
	private UacMenuService uacMenuService;
	@Resource
	private CmnyMessageSource cmnyMessageSource;

	/**
	 * 查看角色信息.
	 *
	 * @param id the id
	 *
	 * @return the wrapper
	 */
	@PostMapping(value = "/queryRoleInfoById/{id}")
	@ApiOperation(httpMethod = "POST", value = "查看角色信息")
	public Wrapper<UacRole> queryRoleInfo(@PathVariable Long id) {
		UacRole role = uacRoleService.selectByKey(id);
		return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, role);
	}

	/**
	 * 验证角色编码是否存在.
	 *
	 * @param checkRoleCodeDto the check role code dto
	 *
	 * @return the wrapper
	 */
	@PostMapping(value = "/checkRoleCode")
	@ApiOperation(httpMethod = "POST", value = "验证角色编码是否存在")
	public Wrapper<Boolean> checkUacRoleCode(@ApiParam(name = "roleCode", value = "角色编码") @RequestBody CheckRoleCodeDto checkRoleCodeDto) {

		logger.info("校验角色编码唯一性 checkRoleCodeDto={}", checkRoleCodeDto);

		boolean stat = uacRoleService.checkUacRoleCode(checkRoleCodeDto);

		//return WrapMapper.ok(stat);
		return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
				.getMessage("operation.success", null), stat);
	}

	/**
	 * 获取权限树
	 *
	 * @param roleId the role id
	 *
	 * @return the auth tree by role id
	 */
	@PostMapping(value = "/getActionTreeByRoleId/{roleId}")
	@ApiOperation(httpMethod = "POST", value = "获取权限树")
	public Wrapper<BindAuthVo> getActionTreeByRoleId(@ApiParam(name = "roleId", value = "角色id") @PathVariable Long roleId) {
		logger.info("roleId={}", roleId);
		BindAuthVo bindAuthVo = uacRoleService.getActionTreeByRoleId(roleId);
		//return WrapMapper.ok(bindAuthVo);
		return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
				.getMessage("operation.success", null), bindAuthVo);
	}

    /**
     * 获取角色菜单列表
     *
     * @param menuQueryDto
     * @return
     */
	@PostMapping(value = "/queryRoleMenuListWithPage")
	public Wrapper queryRoleMenuListWithPage(@RequestBody MenuQueryDto menuQueryDto) {

        PageInfo pageInfo = uacRoleService.queryRoleMenuListWithPage(menuQueryDto);

        //return WrapMapper.ok(pageInfo);
		return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
				.getMessage("operation.success", null), pageInfo);
    }

	/**
	 * 获取角色功能列表
	 *
	 * @param actionMainQueryDto
	 * @return
	 */
	@PostMapping(value = "/queryRoleActionListWithPage")
	public Wrapper queryRoleActionListWithPage(@RequestBody ActionMainQueryDto actionMainQueryDto) {

		PageInfo pageInfo = uacRoleService.queryRoleActionListWithPage(actionMainQueryDto);

		//return WrapMapper.ok(pageInfo);
		return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
				.getMessage("operation.success", null), pageInfo);
	}

	/**
	 * 获取菜单树.
	 *
	 * @param roleId the role id
	 *
	 * @return the menu tree by role id
	 */
	@PostMapping(value = "/getMenuTreeByRoleId/{roleId}")
	@ApiOperation(httpMethod = "POST", value = "获取菜单树")
	public Wrapper<BindAuthVo> getMenuTreeByRoleId(@ApiParam(name = "roleId", value = "角色id") @PathVariable Long roleId) {
		logger.info("roleId={}", roleId);
		BindAuthVo bindAuthVo = uacRoleService.getMenuTreeByRoleId(roleId);
		//return WrapMapper.ok(bindAuthVo);
		return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
				.getMessage("operation.success", null), bindAuthVo);
	}

	@PostMapping(value = "/test")
	public Wrapper test() {

		MenuQueryDto menuQueryDto = new MenuQueryDto();
		menuQueryDto.setPageSize(65535);
		//menuQueryDto.setMenuIds(StringUtils.join(menuIdList, ","));
		menuQueryDto.setMenuIds("1,9,10,15,20,21,23,34,111,1111,1112,1113,1114,1115");
		List<UacMenu> uacMenus = uacMenuService.queryMenuList(menuQueryDto);

		//return WrapMapper.ok(uacMenus);
		return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
				.getMessage("operation.success", null), uacMenus);
	}

}
